package com.example.designmodel;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class DecoratorActivity extends AppCompatActivity {
    public static void launch(Context context) {
        Intent intent = new Intent(context, DecoratorActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_decorator);
        decorator();
        scro();
    }

    private void decorator(){
        Component component = new ConcreteComponent();
        component = new ConcreteDecorator1(component);
        component = new ConcreteDecorator2(component);
        component.operate();
    }

    private void scro(){
        SchoolReport schoolReport = new FourceGradeSchool();
        schoolReport = new HeightestScroeDecorter(schoolReport);
        schoolReport = new SortDecorder(schoolReport);
        schoolReport.report();
    }

    public static abstract class Component{
        public abstract  void operate();
    }

    public static class ConcreteComponent extends Component{
        @Override
        public void operate() {
            Log.i("test","do something dd");

        }
    }

    public static abstract class Decorator extends Component{
        private Component component;

        //通过构造函数传递被修饰者
        public Decorator(Component component) {
            this.component = component;
        }

        //委托给被修饰者执行
        @Override
        public void operate() {
            component.operate();
        }
    }

    public static class ConcreteDecorator1 extends Decorator{
        //定义被修饰者
        public ConcreteDecorator1(Component component) {
            super(component);
        }
        //定义自己的修饰方法
        private void methord1(){
            Log.i("test","decorator1 method1");
        }

        //重写父类的 operate 方法
        @Override
        public void operate() {
            this.methord1();
            super.operate();
        }
    }

    public static class ConcreteDecorator2 extends Decorator{
        //定义被修饰者
        public ConcreteDecorator2(Component component) {
            super(component);
        }
        //定义自己的修饰方法
        private void methord2(){
            Log.i("test","decorator2 method2");
        }
        //重写父类的 operate 方法
        @Override
        public void operate() {
            this.methord2();
            super.operate();
        }
    }

    //学习成绩装饰

    public static abstract class SchoolReport{
         public abstract void report();
         public abstract void sign(String sign);
    }

    public static class FourceGradeSchool extends SchoolReport{
        @Override
        public void report() {
            Log.i("test","成绩单 数学 78 语文87 自然 76");
        }

        @Override
        public void sign(String sign) {
            Log.i("test","sign = "+sign);
        }
    }

    public static abstract class SchollReporeDecorator extends FourceGradeSchool{
        private SchoolReport schoolReport;

        public SchollReporeDecorator(SchoolReport schoolReport) {
            this.schoolReport = schoolReport;
        }

        @Override
        public void report() {
            schoolReport.report();
        }
    }

    public static class HeightestScroeDecorter extends SchollReporeDecorator{

        public HeightestScroeDecorter(SchoolReport schoolReport) {
            super(schoolReport);
        }

        private void heightScroe(){
            Log.i("test","最高分 88");
        }

        @Override
        public void report() {
            heightScroe();
            super.report();
        }
    }

    public static class SortDecorder extends SchollReporeDecorator{

        public SortDecorder(SchoolReport schoolReport) {
            super(schoolReport);
        }

        private void sort(){
            Log.i("test","我的排名是 39位");
        }

        @Override
        public void report() {
            this.sort();
            super.report();
        }
    }

}
